package com.wu.controller;

import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wuyanshen
 * @date 2019-01-14 12:37 AM
 * @discription 描述
 */
//@ControllerAdvice
public abstract class BaseController {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    /** 基于@ExceptionHandler异常处理 */
    @ExceptionHandler
    public String exp(HttpServletRequest request, Exception ex, Model model) {
        request.setAttribute("ex", ex);
        // 根据不同错误转向不同页面
        if(ex instanceof UnknownAccountException) {
            model.addAttribute("msg", "用户不存在");
            return "login";
        }else if(ex instanceof LockedAccountException) {
            model.addAttribute("msg", "用户被锁定");
            return "login";
        } else if(ex instanceof UnauthorizedException) {
            return "401";
        } else {
            return "error";
        }
    }
}
